package com.project.baseApp;

import java.io.UnsupportedEncodingException;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import com.support.volley.AuthFailureError;
import com.support.volley.NetworkResponse;
import com.support.volley.ParseError;
import com.support.volley.Request;
import com.support.volley.Response;
import com.support.volley.Response.ErrorListener;
import com.support.volley.Response.Listener;
import com.support.volley.toolbox.HttpHeaderParser;

public class BaseRequest extends Request<JSONObject> {

	private final Listener<JSONObject> listener;
	private final Map<String, String> map;

	// public BaseRequest(int method, String url, Listener<JSONObject> listener,
	// ErrorListener errorListener) {
	// super(method, url, errorListener);
	// this.listener = listener;
	// setShouldCache(false);
	// }

	public BaseRequest(int method, String url, Map<String, String> map, Listener<JSONObject> listener,
			ErrorListener errorListener) {
		super(method, url, errorListener);
		this.listener = listener;
		this.map = map;
		setShouldCache(false);
	}

	@Override
	protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
		try {
			String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
			return Response.success(new JSONObject(jsonString), HttpHeaderParser.parseCacheHeaders(response));
		} catch (UnsupportedEncodingException e) {
			return Response.error(new ParseError(e));
		} catch (JSONException je) {
			return Response.error(new ParseError(je));
		}
	}

	@Override
	protected void deliverResponse(JSONObject response) {

		listener.onResponse(response);
	}

	@Override
	protected Map<String, String> getParams() throws AuthFailureError {
		return map;
	}

}
